package com.nageoffer.shortlink.project.service.impl;

import com.nageoffer.shortlink.project.service.ShortLinkUrlTitleService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ShortLinkUrlTitleServiceImpl implements ShortLinkUrlTitleService {

    @Override
    public String getUrlTitle(String url) {
        try {
            // 通过Jsoup连接目标网址并解析HTML页面
            Document doc = Jsoup.connect(url).get();
            // 获取页面标题
            Element titleElement = doc.select("title").first();
            return titleElement != null ? titleElement.text() : "No title found";
        } catch (IOException e) {
            // 错误处理
            return "Error fetching title";
        }
    }
}